<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        div{
            width:100px;
            height: 100px;
            background:orange;
            transform:translate(100px,200px);
        }
        ul{
            width:156px;
            height: 100px;
            position: relative;
            position: fixed;
            top: 0;bottom:0;
            left:0;right:0;
            margin: auto;
            padding-top:51px;
        }
        li{
            width: 50px;
            height: 50px;
            border:1px solid orange;
            list-style: none;
            text-align: center;
            line-height: 50px;
            float:left
        }
        li:first-child{
            position: absolute;
            top:0;left:52px;
        }
    </style>
</head>
<body>
    <div></div>
    <!-- <ul>
        <li data-dic="ArrowUp">↑</li>
        <li data-dic="ArrowLeft">←</li>
        <li data-dic="ArrowDown">↓</li>
        <li data-dic="ArrowRight">→</li>
    </ul> -->
</body>
</html>
<script>
    const box = document.querySelector("div");
    let offsetX = 0,offsetY = 0;  
    box.addEventListener("mousedown",event);
    function event(e){
        offsetX = e.offsetX; 
        offsetY = e.offsetY;
        window.onmousemove = function(e){
            let left = e.clientX - offsetX;
            let top = e.clientY - offsetY;
            let width = window.innerWidth;
            let height = window.innerHeight;
            if(left <= 0){
                left = 0; 
            }
            if(top <= 0){
                top = 0;
            }
            if(left >= width-100){
                left = width-100;
            }
            if(top >= height-100){
                top = height-100;
            }
            box.style.transform=`translate(${left}px,${top}px)`;
        }
    }
    window.addEventListener("mouseup",e=>{
        window.onmousemove = null;
    });






    // const lis = document.querySelectorAll("li");
    // lis.forEach(li=>{
    //     li.addEventListener("click",event=>{
    //         lis.forEach(event=>{
    //             event.style.background = "white";
    //         })
    //         li.style.background = "orange";
    //     })
    // })
    // document.addEventListener("keydown",event=>{
    //     lis.forEach(li=>{
    //         if(li.dataset.dic === event.key){
    //             li.style.background = "orange";
    //         }  
    //     })
    // })
    // document.addEventListener("keyup",e=>{
    //     lis.forEach(li=>{
    //         li.style.background = "white";
    //     })
    // })
    


</script>